﻿using System;
using System.Collections.Generic;

namespace Kryptonalysis.BletchleyPark
{
    /// <summary>
    /// Klass to determin greatest common divisor.
    /// </summary>
    public class GCD
    {
        /// <summary>
        /// Determines the GCD of a integer-value-list.
        /// </summary>
        /// <param name="values">The values.</param>
        /// <returns></returns>
        public static int DetermineGCD(List<int> values)
        {
            int n = values[0];
            int m = values[1];
            int count = 0;

            int divisor = ggT(n, m);

            foreach (int currVal in values)
            {
                if (Math.IEEERemainder(currVal, divisor) != 0)
                {
                    count++;
                }
            }
            //Console.WriteLine(count);

            return divisor;
        }

        /// <summary>
        /// Determines the GCD of two values.
        /// </summary>
        /// <param name="n">The n.</param>
        /// <param name="m">The m.</param>
        /// <returns></returns>
        public static int ggT(int n, int m)
        {
            return n == m ? n : n < m ? ggT(n, m - n) : ggT(n - m, m);
        }
    }
}
